package com.atguigu.tingshu.album.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.cache.RedisCache;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	@Override
	@RedisCache(prefix = "album:category:all")
	public List<JSONObject> getBaseCategoryList() {
		//创建响应结果集对象,封装所有一级分类对象
		List<JSONObject> result = new ArrayList<>();
		//查询所有数据
		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
		//按照一级分类进行分组
		Map<Long, List<BaseCategoryView>> map1 = baseCategoryViews.stream()
				.collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		Set<Map.Entry<Long, List<BaseCategoryView>>> entries1 = map1.entrySet();
		//遍历按一级分类分组后的map
		for (Map.Entry<Long, List<BaseCategoryView>> entry1 : entries1) {
			//封装一级分类对象
			JSONObject jsonObject1 = new JSONObject();
			jsonObject1.put("categoryId",entry1.getKey());
			jsonObject1.put("categoryName", entry1.getValue().get(0).getCategory1Name());


			//处理二级分类数据
			List<JSONObject> list2 = new ArrayList<>();
			Map<Long, List<BaseCategoryView>> map2 = entry1.getValue().stream()
					.collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Set<Map.Entry<Long, List<BaseCategoryView>>> entries2 = map2.entrySet();
			for (Map.Entry<Long, List<BaseCategoryView>> entry2 : entries2) {
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("categoryId", entry2.getKey());
				jsonObject2.put("categoryName", entry2.getValue().get(0).getCategory2Name());


				//处理三级分类数据
				List<JSONObject> list3 = new ArrayList<>();
				Map<Long, List<BaseCategoryView>> map3 = entry2.getValue().stream()
						.collect(Collectors.groupingBy(BaseCategoryView::getCategory3Id));
				Set<Map.Entry<Long, List<BaseCategoryView>>> entries3 = map3.entrySet();
				for (Map.Entry<Long, List<BaseCategoryView>> entry3 : entries3) {
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryId", entry3.getKey());
					jsonObject3.put("categoryName", entry3.getValue().get(0).getCategory3Name());
					list3.add(jsonObject3);
				}
				jsonObject2.put("categoryChild",list3);
				list2.add(jsonObject2);
			}
			jsonObject1.put("categoryChild",list2);
			result.add(jsonObject1);
		}
		return result;
	}

	@Override
	@RedisCache(prefix = "category:baseAttribute:")
	public List<BaseAttribute> findAttributeByCategory1Id(Long category1Id) {
		List<BaseAttribute> result = baseAttributeMapper.findAttributeByCategory1Id(category1Id);

		return result;
	}

	@Override
	@RedisCache(prefix ="category:categoryview:")
	public BaseCategoryView getCategoryViewByCategory3Id(Long category3Id) {
		return baseCategoryViewMapper.selectById(category3Id);
	}

	/**
	 * 根据一级分类Id查询置顶7个三级分类列表
	 * @param category1Id
	 * @return
	 */
	@Override
	@RedisCache(prefix = "category:topcategory3")
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		//先根据category1Id获取到二级分类id列表
		List<BaseCategory2> category2List = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>()
				.eq(BaseCategory2::getCategory1Id, category1Id)
				.select(BaseCategory2::getId));
		if (!CollectionUtils.isEmpty(category2List)) {
			List<Long> category2IdList = category2List.stream().map(BaseCategory2::getId)
					.collect(Collectors.toList());
			//根据二级分类ID查询置顶前七个三级分类列表
			LambdaQueryWrapper<BaseCategory3> wrapper = new LambdaQueryWrapper<>();
			wrapper.in(BaseCategory3::getCategory2Id,category2IdList)
					.eq(BaseCategory3::getIsTop,1)
					.orderByAsc(BaseCategory3::getOrderNum)
					.last("limit 7");
			List<BaseCategory3> category3List = baseCategory3Mapper.selectList(wrapper);
			return category3List;
		}
		return null;
	}

	/**
	 * 查询1级分类下包含所有二级以及三级分类
	 * @param category1Id
	 * @return
	 */
	@Override
	@RedisCache(prefix ="category:categoryby1Id:")
	public JSONObject getBaseCategoryList(Long category1Id) {
		//根据一级分类id查询所有的分类列表
		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>()
				.eq(BaseCategoryView::getCategory1Id, category1Id));
		if(!CollectionUtils.isEmpty(baseCategoryViews)){
			JSONObject jsonObject = new JSONObject();
			//创建一级分类对象
			BaseCategoryView baseCategoryView = baseCategoryViews.get(0);
			//封装一级分类对象
			jsonObject.put("category1Id",baseCategoryView.getCategory1Id());
			jsonObject.put("categoryName",baseCategoryView.getCategory1Name());

			//处理二级分类对象
			Map<Long, List<BaseCategoryView>> map2 = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			//创建二级分类JSON对象集合
			List<JSONObject> jsonObject2List = new ArrayList<>();
			for (Map.Entry<Long, List<BaseCategoryView>> entry : map2.entrySet()) {
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("categoryId",entry.getKey());
				jsonObject2.put("categoryName",entry.getValue().get(0).getCategory2Name());
				//创建三级分类JSON对象集合
				List<JSONObject> jsonObject3List = new ArrayList<>();
				//遍历二级分类列表,封装三级分类对象
				for (BaseCategoryView categoryView : entry.getValue()) {
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryId", categoryView.getCategory3Id());
					jsonObject3.put("categoryName",categoryView.getCategory3Name());
					jsonObject3List.add(jsonObject3);
				}
				jsonObject2.put("categoryChild",jsonObject3List);
				jsonObject2List.add(jsonObject2);
			}
			jsonObject.put("categoryChild",jsonObject2List);
			return jsonObject;
		}
		return null;
	}


}
